package cn.edu.ujn.zzylab4.service;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import cn.edu.ujn.zzylab4.dao.Student;
import cn.edu.ujn.zzylab4.dao.User;

/**
 * Session Bean implementation class StudentFacade
 */
@Stateless
@LocalBean
public class StudentFacade implements StudentFacadeRemote {
	@PersistenceContext
	private EntityManager entityManager;
    /**
     * Default constructor. 
     */
    public StudentFacade() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public Student addStudent(Student stu) {
		// TODO Auto-generated method stub
		stu.setSname("zzy");
		stu.setSnumber("20181111");
		stu.setSage(20);
		entityManager.persist(stu);
		
		return stu;
	}

	@Override
	public Student find(int id) {
		Query query = entityManager.createQuery("from Student where id=:id");
	
		List<Student> list = query.getResultList();
		if(list.size()>0)
			return list.get(0);
		else
			return null;
	}

	@Override
	public Student update(Student student) {
		// TODO Auto-generated method stub
		student.setSname("zzzy");
		student.setSage(21);
		student.setSnumber("2222222");
		entityManager.persist(student);
		return student;
	}

}
